package com.itheima.app.manager;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Notification;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.User;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.SettingVo;
import com.itheima.service.db.BlackListService;
import com.itheima.service.db.NotificationService;
import com.itheima.service.db.QuestionService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

@Component
public class SettingManager {
    @DubboReference
    private QuestionService questionService;

    @DubboReference
    private NotificationService notificationService;


    public ResponseEntity findSettings() {
        User user = UserHolder.getLocal();
        Question question = questionService.findByUserId(user.getId());
        Notification notification = notificationService.findByUserId(user.getId());

        SettingVo settingVo = new SettingVo();
        settingVo.setId(user.getId());
        settingVo.setPhone(user.getMobile());
        if (question != null) {
            settingVo.setStrangerQuestion(question.getStrangerQuestion());
        }
        if (notification != null) {
            settingVo.setGonggaoNotification(notification.getGonggaoNotification());
            settingVo.setPinglunNotification(notification.getPinglunNotification());
            settingVo.setLikeNotification(notification.getLikeNotification());
        }
        return ResponseEntity.ok(settingVo);
    }

    public ResponseEntity saveOrUpdateQuestions(String content) {
        Long localUserId = UserHolder.getLocalUserId();
        Question question = questionService.findByUserId(localUserId);
        if (question == null) {  //新用户数据库无问题
            question = new Question();

            question.setUserId(localUserId);
            question.setStrangerQuestion(content);

            questionService.save(question);
        } else {

            question.setStrangerQuestion(content);
            questionService.update(question);
        }

        return null;
    }

    public ResponseEntity saveOrUpdateNotifications(Notification notification) {
        Notification notificationByUserId = notificationService.findByUserId(UserHolder.getLocalUserId());
        //一定存在不用new 但是传递的notification没有id和userid
        notification.setUserId(UserHolder.getLocalUserId());
        System.out.println("manager--notification----" + notification);
        if (notificationByUserId == null) {
            notificationService.save(notification);
        } else {
            System.out.println("进入修改");
            notification.setId(notificationByUserId.getId());
            notificationService.update(notification);
        }
        return null;
    }

    @DubboReference
    private BlackListService blackListService;

    public ResponseEntity findBlacklist(Integer page, Integer pagesize) {
        System.out.println("1");
        Long localUserId = UserHolder.getLocalUserId();
        PageBeanVo byPage = blackListService.findByPage(page, pagesize, localUserId);
        return ResponseEntity.ok(byPage);
    }

    public ResponseEntity deleteBlackUser(Long blackUserId) {
        Long localUserId = UserHolder.getLocalUserId();
//        System.out.println("localUserId"+localUserId);
//        System.out.println("blackUserId"+blackUserId);
        blackListService.delete(localUserId,blackUserId);
        return null;
    }
}
